package org.chnxi.study.leetcode.questions25.ques25;

/**
 * 未完成
 */
public class Simple01 {

    public static void main(String[] args) {

        ListNode ln1 = new ListNode(1);
        ListNode ln2 = new ListNode(2);
        ListNode ln3 = new ListNode(3);
        ListNode ln4 = new ListNode(4);
        ListNode ln5 = new ListNode(5);
        ln1.next = ln2;
        ln2.next = ln3;
        ln3.next = ln4;
        ln4.next = ln5;

        ListNode r = reverseKGroup(ln1 , 2);
        System.out.println(r);

    }

    public static ListNode reverseKGroup(ListNode head, int k) {
        ListNode newHead = new ListNode(0);
        newHead.next = head;
        ListNode tail = newHead;
        ListNode newTail = newHead;
        while (true){
            int count = 0;
            while (tail != null && count != k){
                tail = tail.next;
                count ++;
            }
            if(tail == null){
                break;
            }
            System.out.println(tail);

            while (newTail.next != tail){

            }
        }

        return null;
    }

    public static ListNode reverseKGroup2(ListNode head, int k) {

        ListNode tail = head;
        int count = 0;

        ListNode newHead = new ListNode(0);
        ListNode newTail = head;

        while (tail != null && count <= k){

            if(count == k){
                newTail.next = tail;
            }else{
                ListNode tmp = tail;
                ListNode next = tail.next;
                tail.next = null;

                ListNode newNext = newHead.next;
                newHead.next = tmp;
                tmp.next = newNext;

                tail = next;
            }
            count++;
        }

        return newHead.next;
    }

    private static void reverse(ListNode node){
    }

    public static ListNode reverseKGroup1(ListNode head, int k) {
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode pre = dummy;
        ListNode tail = dummy;
        while (true) {
            int count = 0;
            while (tail != null && count != k) {
                count++;
                tail = tail.next;
            }
            if (tail == null) break;
            System.out.println(tail);
            ListNode head1 = pre.next;
            while (pre.next != tail) {
                ListNode cur = pre.next;
                pre.next = cur.next;
                cur.next = tail.next;
                tail.next = cur;
            }
            pre = head1;
            tail = head1;
        }
        return dummy.next;
    }

}
